app.register.controller('PartStockDetailCtrl', function($scope, $rootScope,
		PartStockDetailSer, AppUtil, $http, $q,$location,$routeParams) {
	
	loadFlag($routeParams.id,$scope);
	
	$scope.pagination = {
			currentPage : page =1,
			itemsPerPage : size =10
	};

	$scope.SearchData = function() {
		$scope.postData={};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		// 表单查询参数
		LoadList();
	};
	
	// 加载数据方法
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, $scope.selected,params);
		params["storage_text"] = angular.element('select[name=storage_code]').find("option:selected").text();
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			if($scope.entity_name != null){
				params['entity_name'] = $scope.entity_name;
			}
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		
		var start_age = $("#start_age").val();
		var end_age = $("#end_age").val();
		if(start_age!='' && !checkPositiveNumber(start_age)){
			alert('库龄范围必须是大于或等于0的整数');
			return false;
		}
		if(end_age!='' && !checkPositiveNumber(end_age)){
			alert('库龄范围必须是大于或等于0的整数');
			return false;
		}
		
		if(start_age!='' && end_age!=''){
			if(Number(start_age)>Number(end_age)){
				alert('库龄范围起始数字必须小于等于截止数字');
				return false;
			}
		}
		
		// 加载数据
		PartStockDetailSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.partStockDetailReports = response.data.data;
			$scope.partStockDetailReportsSum = response.data.sum;
			setTimeout(check_table_tr,500);
		});
	}
	
	// 配置分页监听
	$scope.$watch('pagination.refresh', LoadList);
	
	//加载查询条件
	$http.get("partStockDetail/dicts").success(function(data){
		$scope.part_status = data.data.isornot;
		$scope.partmain_list = data.data.partmain_list;
		$scope.part_type = data.data.part_type;
		$scope.storage_list = data.data.storage_list;
		$scope.sub_list = data.data.sub_list;
		$scope.third_list = data.data.third_list;
		$scope.fourth_list = data.data.fourth_list;
		
		$scope.pre_sub_list=data.data.sub_list;
		$scope.pre_third_list=data.data.third_list;
		$scope.pre_fourth_list=data.data.fourth_list;
		$scope.pre_storage_list=data.data.storage_list;
	});
	
	$scope.LoadEntityLevel = function(entity) {
		$scope.currentEntity= entity.entity_code;
		$rootScope.search_entity_name = entity.entity_name;
		
		$scope.subGroupName = '';
		$scope.thirdGroupName = '';
		$scope.subGroupName = '';
		$scope.storageName = '';
		
		$scope.sub_list=null;
		$scope.third_list_sel=null;
		$scope.fourth_list_sel=null;
		$scope.storage_list=null;
		//加载下拉列表数据
		$http.get("partStockDetail/dicts?es="+entity.entity_code).success(function(data){
			$scope.sub_list=data.data.sub_list;
			$scope.third_list=data.data.third_list;
			$scope.fourth_list=data.data.fourth_list;
			$scope.storage_list=data.data.storage_list;
		})
		
		$("#subgroupname").val('');
		$("#thirdgroupname").val('');
		$("#fourthgroupname").val('');
		$("#storage_name").val('');
	}
	
	//清空门店
	$scope.clearGroupBrandEntity = function(){
		$rootScope.search_entity_name='';
		
		$scope.subGroupName = '';
		$scope.thirdGroupName = '';
		$scope.subGroupName = '';
		$scope.storageName = '';
		
		$scope.sub_list=null;
		$scope.third_list_sel=null;
		$scope.fourth_list_sel=null;
		$scope.storage_list=null;
		
		$scope.sub_list=$scope.pre_sub_list;
		$scope.third_list=$scope.pre_third_list;
		$scope.fourth_list=$scope.pre_fourth_list;
		$scope.storage_list=$scope.pre_storage_list;
	}
	
	//门店监听
	$rootScope.$watch('search_entity_name', function(newValue,oldValue,scope){
		if(newValue==''){
			$scope.clearGroupBrandEntity();
		}
	});
	
	AppUtil.LoadGroups();
	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};
	
    $scope.formatStr = function(input){
		var returnStr = "";
		if(input == undefined){
			return;
		}
		var itemArr = input.split(";");
		for (var i = 0; i < itemArr.length; i++) {
			var temp = itemArr[i].split(":");
			if(temp.length>1){
				returnStr += temp[1];
			}else{
				returnStr += itemArr[i];
			}
		}
	    return returnStr;
	}
    
    $scope.loadThird=function(name){
		$scope.third_list_sel=null;
		$scope.fourth_list_sel=null;
		$scope.thirdGroupName = '';
		var thirdList=new Array();
		var sub_code='';
		$.each($scope.sub_list,function(i,o){
			if(o.name==name){sub_code=o.code;}
		})
		if(sub_code!=''){
			$.each($scope.third_list,function(i,o){
				if(o.sub_code==sub_code){
					thirdList.push(o);
				}
			})
			$scope.third_list_sel=thirdList;
		}
	}
	
	
	$scope.loadFourth=function(name){
		$scope.fourth_list_sel=null;
		var fourthList=new Array();
		var sub_code='';
		$.each($scope.third_list,function(i,o){
			if(o.name==name){sub_code=o.code;}
		})
		if(sub_code!=''){
			$.each($scope.fourth_list,function(i,o){
				if(o.sub_code==sub_code){
					fourthList.push(o);
				}
			})
			$scope.fourth_list_sel=fourthList;
		}
	}
	
	// 验证正整数包括0 
	function checkPositiveNumber(value){
	    var bool= /^(0|[1-9][0-9]*)$/.test(value);
	    return bool;
	} 
});
app.register.service('PartStockDetailSer', function($http, AppUtil) {

	// 查询数据
	this.list = function(postData) {
		AppUtil.loading();
		postData.start = (postData.page - 1 < 0 ? 0 : postData.page - 1)
				* postData.size;
		return $http.post('partStockDetail/list', postData).error(function() {
			alertMsg("提示", "系统出错,请稍后重试.");
			AppUtil.remove_loading();
		});
	};
	
	
});
